home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 054 (1988-05-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 054 (1988-05-15)(Ossowski, Stefan)(DE)(PD).adf / MRBackup / MRBackup2.0 / Timer.c < prev    next >
C/C++ Source or Header  |  1988-04-09  |  4KB  |  161 lines

  1. /* Timer device support routines.
  2.  * Filename:    Timer.c
  3.  * Author:        Mark R. Rinfret
  4.  * Date:        11/29/87
  5.  *
  6.  */
  7.  
  8. #include ":src/lib/Timer.h"
  9. #include <exec/memory.h>
  10.  
  11. /* Allocate and prepare a timer request structure. 
  12.  * Called with:
  13.  *        vBlank:    1 => vertical blanking timer, microhertz timer otherwise
  14.  * Returns:
  15.  *        pointer to timer request or NULL (failed)
  16.  */
  17.  
  18. struct timerequest *
  19. CreateTimer(vBlank)
  20.     BOOL vBlank;
  21. {
  22.     int status = 0;
  23.     struct MsgPort  *timerPort = NULL;
  24.     struct timerequest *timeRequest = NULL;
  25.     ULONG timerType;
  26.  
  27.     timerType = (vBlank ? UNIT_VBLANK : UNIT_MICROHZ);
  28.  
  29.     if ( timerPort = CreatePort(0L, 0L) ) {
  30.         if ( ! (timeRequest = (struct timerequest *)
  31.             AllocMem((long) sizeof(struct timerequest),
  32.                      MEMF_CLEAR | MEMF_PUBLIC) ) ) {
  33. killport:
  34.             DeletePort(timerPort);
  35.         }
  36.         else {
  37.             timeRequest->tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  38.             timeRequest->tr_node.io_Message.mn_Node.ln_Pri = 0;
  39.             timeRequest->tr_node.io_Message.mn_ReplyPort = timerPort;
  40.             if (OpenDevice(TIMERNAME, timerType, timeRequest, 1L) ) {
  41.                 DeleteTimer(timeRequest);
  42.                 timeRequest = NULL;
  43.                 goto killport;
  44.             }
  45.         }
  46.     }
  47.     return timeRequest;
  48. }
  49.  
  50. /* Delete a timer request created by CreateTimer().
  51.  * Called with:
  52.  *        timeRequest:    pointer to timer request structure
  53.  */
  54.  
  55. void
  56. DeleteTimer(timeRequest)
  57.     struct timerequest *timeRequest;
  58. {
  59.     struct MsgPort *msgPort;
  60.  
  61.     msgPort = timeRequest->tr_node.io_Message.mn_ReplyPort;
  62.  
  63.     if (timeRequest->tr_node.io_Device) {
  64.         AbortIO(timeRequest);
  65.         GetMsg(msgPort);
  66.         CloseDevice(timeRequest);
  67.     }
  68.     FreeMem(timeRequest, (long) sizeof(timeRequest));
  69.     DeletePort(msgPort);
  70. }
  71.  
  72. /* Start an asynchronous timer request.  The user application detects
  73.  * the expiration of the timer with Wait(timeRequest->ReplyPort->mp_SigBit)
  74.  * or WaitIO(timeRequest).
  75.  * Called with:
  76.  *        timeRequest:    pointer to timer I/O request block
  77.  *        seconds:        number of seconds in time interval
  78.  *        microSeconds:    number of uSecs in time interval
  79.  *
  80.  * Caution:    seconds, microSeconds are ULONG parameters!
  81.  */
  82.  
  83. void
  84. StartTimer(timeRequest, seconds, microSeconds)
  85.     struct timerequest *timeRequest; ULONG seconds, microSeconds;
  86. {
  87.     timeRequest->tr_time.tv_secs = seconds;
  88.     timeRequest->tr_time.tv_micro = microSeconds;
  89.     timeRequest->tr_node.io_Command = TR_ADDREQUEST;
  90.     timeRequest->tr_node.io_Flags = 0;
  91.     timeRequest->tr_node.io_Error = 0;
  92.     SendIO(timeRequest);                /* start the timer */
  93. }
  94.  
  95. /* Stop an asynchronous timer request.
  96.  * Called with:
  97.  *        timeRequest:    pointer to timer I/O request block
  98.  */
  99.  
  100. void
  101. StopTimer(timeRequest)
  102.     struct timerequest *timeRequest;
  103. {
  104.     AbortIO(timeRequest);
  105.     WaitIO(timeRequest);
  106. }
  107.  
  108. #ifdef DEBUG
  109. #define SHORTBIT (1L<<shortTimer->tr_node.io_Message.mn_ReplyPort->mp_SigBit)
  110. #define LONGBIT  (1L<<longTimer->tr_node.io_Message.mn_ReplyPort->mp_SigBit)
  111. main()
  112. {
  113.     unsigned intervals;
  114.     struct timerequest *shortTimer, *longTimer;
  115.     ULONG signals;
  116.  
  117.     /* This simple program example sets up two timers of different
  118.      * intervals and reports their expirations.
  119.      */
  120.  
  121.     puts("This example defines two timers.  The first has an interval of");
  122.     puts("five seconds, while the second has an interval of 10 seconds.");
  123.     puts("The expiration of each timer is reported to the screen until");
  124.     puts("10 intervals have been detected.\n");
  125.  
  126.     if (! (shortTimer = CreateTimer(0) ) ) {
  127.         puts("Failed to create short interval timer!");
  128.         exit();
  129.     }
  130.  
  131.     if (! (longTimer = CreateTimer(0) ) ) {
  132.         puts("Failed to create long interval timer!");
  133.         DeleteTimer(shortTimer);
  134.         exit();
  135.     }
  136.  
  137.     StartTimer(shortTimer, 5L, 0L);
  138.     StartTimer(longTimer, 10L, 0L);
  139.     for (intervals = 0; intervals < 10; ) {
  140.         printf("Begin wait %2d ... ", intervals);
  141.         signals = Wait(SHORTBIT | LONGBIT);
  142.         puts("end wait.");
  143.         if (signals & SHORTBIT) {
  144.             GetMsg(shortTimer->tr_node.io_Message.mn_ReplyPort);
  145.             ++intervals;
  146.             puts("  Short interval timer expired.");
  147.             StartTimer(shortTimer, 5L, 0L);
  148.         }
  149.         if (signals & LONGBIT) {
  150.             GetMsg(longTimer->tr_node.io_Message.mn_ReplyPort);
  151.             ++intervals;
  152.             puts("   Long interval timer expired.");
  153.             StartTimer(longTimer, 10L, 0L);
  154.         }
  155.     }
  156.     DeleteTimer(shortTimer);
  157.     DeleteTimer(longTimer);
  158.     puts("\nEnd of timer demo.\n");
  159. }
  160. #endif
  161.